//
//  LBMBaseNavController.swift
//  微博
//
//  Created by 刘保满 on 16/5/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class LBMBaseNavController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //解决右滑手势失效的问题 但是如果只这样写还会有bug,顶级视图还可以右滑
        self.interactivePopGestureRecognizer?.delegate = self
        
        
        
    }

    //手势代理方法什么时候开始
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        //当子控制器的数目大于1 时 就让它开始 否则,失效
//        if childViewControllers.count > 1 {
//            
//            return true
//        }
//        
//        return false
        
        //可以对上面的几句代码进行简写
        return childViewControllers.count > 1
        
    }
    
    
    
    //MARK:实现一个功能:当从首页第一次push 到下一个控制器时,让下一个控制器的左侧按钮显示的文字和首页的标题一致,以后再向下一级跳,左侧的按钮都显示  "返回"
    
    //重写push方法
    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        
        var backTitle = "返回"
        if childViewControllers.count > 0 {
        
            if childViewControllers.count == 1 {
                
                //当子控制器的个数为1时,让他的返回按钮的标题和顶级视图的标题一致
                backTitle = childViewControllers.first?.title ?? "返回"

            }
            
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(popToFrontView))
            
            viewController.navigationItem.leftBarButtonItem = back
            
            
            
            
        }
        
//        print(childViewControllers.count)
        super.pushViewController(viewController, animated: true)
        
//        print(childViewControllers.count)

    }
    
    
    //返回
    @objc private func popToFrontView() {
        
        popViewControllerAnimated(true)
        
    }
    


}
